
/*
 * Copyright (c) 2010 Sergey N Frolov
 * 
 * See LICENSE file
 */

#ifndef _NGX_JAVASCRIPT_MODULE_MYSQL_H_INCLUDED_
#define _NGX_JAVASCRIPT_MODULE_MYSQL_H_INCLUDED_

#include "ngx_javascript.h"

#include <mysql.h>

typedef struct {
	MYSQL_STMT *st;
	char *pool;
	MYSQL_BIND *bind_param;
	MYSQL_BIND *bind_field;
	my_bool *is_null;
	unsigned long *length;
	void *buffer;
	unsigned int param_count;
	unsigned int field_count;
	my_bool update_max_length;
	unsigned long cursor_type;
} ngx_javascript_mysql_result_t;

JSBool ngx_javascript_init_mysql(JSContext *jcx, JSObject *global);

void ngx_javascript_mysql_finalize(JSContext *jcx, JSObject *obj);
JSBool ngx_javascript_mysql(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_prepare(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_execute(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_quote(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_escape(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_close(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);

JSBool ngx_javascript_mysql_query(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval, int todo);

void ngx_javascript_mysql_result_finalize(JSContext *jcx, JSObject *obj);
JSBool ngx_javascript_mysql_param_bind(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_result_execute(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_result_length(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_result_fetchArray(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_result_fetchObject(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
JSBool ngx_javascript_mysql_result_finish(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);

jsval ngx_javascript_mysql_get_value(JSContext *jcx, MYSQL_BIND *bind);

JSBool ngx_javascript_mysql_result_fetch_value(JSContext *jcx, JSObject *obj, uintN argc, jsval *argv, jsval *rval, int todo);
JSBool ngx_javascript_mysql_param_bind_value(JSContext *jcx, MYSQL_BIND *bind, jsval result);
JSBool ngx_javascript_mysql_result_bind(JSContext *jcx, ngx_javascript_mysql_result_t *pst);

JSBool ngx_javascript_mysql_error(JSContext *jcx, int code, const char *message);

#define NGX_JAVASCRIPT_MYSQL_PREPARE 0
#define NGX_JAVASCRIPT_MYSQL_EXECUTE 1
#define NGX_JAVASCRIPT_MYSQL_FETCH_ARRAY 0
#define NGX_JAVASCRIPT_MYSQL_FETCH_OBJECT 1

#endif /* _NGX_JAVASCRIPT_MODULE_MYSQL_H_INCLUDED_ */
